package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 查询用户资料
     */
    @GetMapping
    public ResponseEntity users(Long userID) {
        if (userID == null) {
            userID = UserHolder.getUserId();
        }
        UserInfoVo userInfo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo) {
        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    //相互喜欢、喜欢和粉丝数据的统计查询。
    @GetMapping("/counts")
    public ResponseEntity counts() {
        Map map = userInfoService.counts();
        return ResponseEntity.ok(map);
    }

    //从粉丝中喜欢
    @PostMapping("/fans/{uid}")
    public ResponseEntity fansLike(@PathVariable("uid") Long uid) {
        userInfoService.fansLike(uid);
        return ResponseEntity.ok(null);
    }

    //从喜欢里 取消喜欢
    @DeleteMapping("/like/{uid}")
    public ResponseEntity deleteLike(@PathVariable("uid") Long uid) {
        userInfoService.deleteLike(uid);
        return ResponseEntity.ok(null);
    }

    @GetMapping("/friends/{type}")
    public ResponseEntity selectList(@PathVariable("type") Integer type,
                                     @RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "10") Integer pagesize,
                                      String nickname) {
        PageResult pageResult = userInfoService.selectList(type,page,pagesize,nickname);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 跟换用户头像
     * @return
     */
    @PostMapping("/header")
    public ResponseEntity header(MultipartFile headPhoto, @RequestHeader("Authorization") String token) throws IOException {

        userInfoService.updateHead(headPhoto,UserHolder.getUserId());

        return ResponseEntity.ok(null);
    }

    /**
     * 更换手机号 发送验证码
     * @param
     * @return
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(@RequestHeader("Authorization") String token){

        userInfoService.sendVerificationCode(token);
        return ResponseEntity.ok(null); //正常返回状态码200
    }

    /**
     * 更换手机号 校验验证码
     * @param
     * @return
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map){


        String verificationCode = (String) map.get("verificationCode");

        Map retmap = userInfoService.checkVerificationCode( verificationCode);

        return ResponseEntity.ok(retmap);
    }

    /**
     * 保存新手机号
     * @param
     * @return
     */
    @PostMapping("/phone")
    public ResponseEntity savenewphone(@RequestBody Map map){
        String phone = (String) map.get("phone");
        userInfoService.savenewphone(phone);

        return ResponseEntity.ok(null); //正常返回状态码200
    }

}
